package com.pengxun.manager.service.system.impl;

import com.alibaba.fastjson.JSON;
import com.pengxun.manager.dao.system.PermissionDao;
import com.pengxun.manager.enums.PermissionCodeTypeEnums;
import com.pengxun.manager.model.system.Menu;
import com.pengxun.manager.model.system.Permission;
import com.pengxun.manager.service.system.MenuService;
import com.pengxun.manager.service.system.PermissionService;
import com.pengxun.manager.service.system.RoleMenuService;
import com.pengxun.manager.vo.PermissionVO;
import com.szeastroc.common.jdbc.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class PermissionServiceImpl implements PermissionService {

    @Resource
    private PermissionDao permissionDao;
    @Resource
    private RoleMenuService roleMenuService;
    @Resource
    private MenuService menuService;

    @Override
    public int insert(Permission permission) {
//		Permission lastPermission = selectLastPermission();
//		if(null==lastPermission) {
//			permission.setPermissionCode(CodeUtils.generateCode("permissionCode", 6, "permissionCode000000"));
//		}else {
//			permission.setPermissionCode(CodeUtils.generateCode("permissionCode", 6, lastPermission.getPermissionCode()));
//		}
        StringBuffer permissionCode = new StringBuffer(permission.getMenuCode());
        if (!permission.getMenuCode().endsWith(".")) {
            permissionCode.append(".");
        }
        permissionCode.append(permission.getPermissionCode());
        permission.setPermissionCode(permissionCode.toString());
        permission.setPermissionType(1);
        if (null == permission.getStatus()) {
            permission.setStatus(1);
        }
        return permissionDao.insert(permission);
    }

    @Override
    public int update(Permission permission) {
        return permissionDao.update(permission);
    }

    @Override
    public int delete(Integer permissionId) {
        Permission permission = findByPermissionId(permissionId);
        //删除角色权限
        roleMenuService.deleteByPermissionCode(permission.getPermissionCode());
        return permissionDao.delete(permissionId);
    }

    @Override
    public Page findByPage(PermissionVO permission) {
        List<Permission> permissions = permissionDao.findByPage(permission);
        Page page = new Page();
        BeanUtils.copyProperties(permission, page);
        page.setData(permissions);
        return page;
    }

    public Permission findByPermissionId(Integer permissionId) {
        return permissionDao.findByPermissionId(permissionId);
    }

    @Override
    public Permission findByPermissionCode(String permissionCode) {
        return permissionDao.findByPermissionCode(permissionCode);
    }

    @Override
    public Permission selectLastPermission() {
        return permissionDao.selectLastPermission();
    }

    @Override
    public List<Permission> findByMenuCode(String menuCode) {
        return permissionDao.findByMenuCode(menuCode);
    }

    @Override
    public int updateStatus(Integer permissionId, Integer status) {
        return permissionDao.updateStatus(permissionId, status);
    }

    @Override
    public int deleteByMenuCode(String menuCode) {
        return permissionDao.deleteByMenuCode(menuCode);
    }

    @Override
    public int deleteAll() {
        return permissionDao.deleteAll();
    }

    @Override
    public void syncPermissions(List<Menu> list) {
        for (Menu firstMenu : list) {
            Menu menu = menuService.findByMenuCode(firstMenu.getMenuCode());
            if (menu == null) {
                menuService.insert(firstMenu);
            }
            if (firstMenu.getMenuType() == 0) {
                //获取二级菜单
                List<Menu> secondMenus = firstMenu.getChildren();
                //没有二级菜单,开始处理一级权限
                if (CollectionUtils.isEmpty(secondMenus)) {
                    processPermission(firstMenu);
                    continue;
                }
                for (Menu secondMenu : secondMenus) {
                    Menu smenu = menuService.findByMenuCode(secondMenu.getMenuCode());
                    if (smenu == null) {
                        menuService.insert(secondMenu);
                    }
                    //获取三级菜单
                    List<Menu> thirdMenus = secondMenu.getChildren();
                    if (CollectionUtils.isEmpty(thirdMenus)) {
                        //没有三级菜单，开始处理二级菜单权限
                        processPermission(secondMenu);
                        continue;
                    }
                    for (Menu thirdMenu : thirdMenus) {
                        Menu tmenu = menuService.findByMenuCode(thirdMenu.getMenuCode());
                        if (tmenu == null) {
                            menuService.insert(thirdMenu);
                        }
                        processPermission(thirdMenu);
                    }
                }
            }
        }

    }
	private void processPermission(Menu menu) {
		List<Permission> thirdPermissions = menu.getPermissions();
		log.info("menu==={}", JSON.toJSONString(menu));
		log.info("thirdPermissions ===> {}", thirdPermissions);
		for (Permission permission : thirdPermissions) {
			log.info("permission==={}",JSON.toJSONString(permission));
			permission.setMenuCode(menu.getMenuCode());
			String permissionCode = permission.getPermissionCode();
			log.info("permissionCode==={}",permissionCode);
			String[] codes = permissionCode.split("\\.");
			String codeType = codes[codes.length-1];
			PermissionCodeTypeEnums enums = PermissionCodeTypeEnums.getEnumByType(codeType);
			String codeTypeName = codeType;
			if(enums!=null) {
				codeTypeName = enums.getCodeTypeName();
			}
			permission.setPermissionName(codeTypeName);
			permissionDao.insert(permission);
		}
	}
}
